package com.zj.service;

import jakarta.mail.internet.MimeMessage;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

@Service
@Log
public class MailService {
    @Autowired
    private JavaMailSender javaMailSender;

    public void send (String to,String from,String subject,String content){
        MimeMessage mm=javaMailSender.createMimeMessage(); //可以包含附件
        try {
        MimeMessageHelper message=new MimeMessageHelper(mm,true,"utf-8");

            message.setTo(to); //设置接收人
            message.setFrom(from); //设置发送人
            message.setSubject(subject); //设置邮件主题
            message.setText(content,true); //设置邮件内容  一定是true，表示发过去的是html
            javaMailSender.send(mm);
        } catch (Exception e) {
            log.severe("发送邮件失败"+e);
        }
    }
}
